package com.homework.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.homework.bean.CourseBean;
import com.homework.bean.HomeworkBean;
import com.homework.bean.UserBean;
import com.homework.service.HomeworkService;
import com.homework.util.PublicMethod;
import com.homework.util.connDB;
import com.opensymphony.xwork2.ActionSupport;
import com.sina.sae.storage.SaeStorage;

public class HomeworkUploadAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HomeworkService homeworkService;
	private int courseId;
	private String homeworkName;
	private String describe;
	private String savePath;
	private String batch;
	private String textfield;

	private File fileField;
	private String fileFieldContentType;
	private String fileFieldFileName;
	private FileInputStream fis;

	public HomeworkUploadAction() {
		homeworkService = new HomeworkService();
	}

	public int getCourseId() {
		return courseId;
	}

	public void setCourseId(int courseId) {
		this.courseId = courseId;
	}

	public String getHomeworkName() {
		return homeworkName;
	}

	public void setHomeworkName(String homeworkName) {
		this.homeworkName = homeworkName;
	}

	public File getFileField() {
		return fileField;
	}

	public void setFileField(File fileField) {
		this.fileField = fileField;
	}

	public String getFileFieldContentType() {
		return fileFieldContentType;
	}

	public void setFileFieldContentType(String fileFieldContentType) {
		this.fileFieldContentType = fileFieldContentType;
	}

	public String getFileFieldFileName() {
		return fileFieldFileName;
	}

	public void setFileFieldFileName(String fileFieldFileName) {
		this.fileFieldFileName = fileFieldFileName;
	}

	public String getDescribe() {
		return describe;
	}

	public void setDescribe(String describe) {
		this.describe = describe;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getBatch() {
		return batch;
	}

	public void setBatch(String batch) {
		this.batch = batch;
	}

	public String getTextfield() {
		return textfield;
	}

	public void setTextfield(String textfield) {
		this.textfield = textfield;
	}

	// 作业上传
	public String homeworkUpload() throws IOException, URISyntaxException, SQLException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		savePath = "/data_homework/homework/"+getHomeworkName()+PublicMethod.getSuffix(getFileFieldFileName());
		
		// 将上传的作业保存到指定位置
		SaeStorage ss = new SaeStorage(connDB.getAccessKey(),connDB.getSecretKey(),connDB.getAppName());

		fis = new FileInputStream(getFileField());
		byte[] buffer = new byte[20*1024*1024];
		int len = 0;
		len = fis.read(buffer);
		byte[] buffer2 = new byte[len];
		for (int i = 0; i < len; i++) {
			buffer2[i] = buffer[i];
		}
		ss.write("homework", savePath, buffer2);
		fis.close();
		
		// userBean
		UserBean userBean = new UserBean();
		userBean.setUserId(String.valueOf(session.getAttribute("userId")));
		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseId(String.valueOf(this.getCourseId()));
		// homeworkBean
		HomeworkBean homeworkBean = new HomeworkBean();
		homeworkBean.setHomeworkName(getHomeworkName());
		homeworkBean.setBatch(getBatch());
		homeworkBean.setAddress(getSavePath());
		homeworkBean.setDescribe(getDescribe());
		int chooseId = homeworkService.getChooseId(userBean, courseBean);
		homeworkBean.setTrack(homeworkService.setDirTrack(chooseId));
		homeworkBean.setHomeworkVic(homeworkService.setDirVic(chooseId));
		//转换成在线作业数据，并存入指定地址
		homeworkService.saveVirt(PublicMethod.getLastString(getFileFieldFileName()),homeworkBean);
		
		// 将作业存放地址存到数据库
		homeworkService.homeworkUpload(userBean, courseBean, homeworkBean);
		request.setAttribute("courseId", getCourseId());
		
		//检查相似度
		homeworkService.similarityCheck(homeworkBean,courseBean);
		
		return "Success";
	}
}
